package com.example.core.email;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName MailController
 * @Author zhangzhaoyang
 * @Date 2019/10/30 16:43
 **/

@RequestMapping("/mail")
@RestController
public class MailController {
    private static Logger logger = LoggerFactory.getLogger(MailController.class);

    /**
     * 发送邮件业务层接口
     */
    @Autowired
    private MailService mailService;
    /**
     *
     * @return
     */
    @RequestMapping(value = "/sendSimpleMail", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView sendSimpleMail(@RequestParam("email") String email, HttpServletResponse response) throws IOException {
        logger.info("===发送普通文本邮件===");
        ModelAndView modelAndView= new ModelAndView();
        //判断页面传来的参数不能为空
        if(StringUtils.isBlank(email)){
//            modelAndView.setViewName("/index");
            //           下面代码是为了方便开发
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('发送普通文本邮件[失败]！"+email+"')</script>");
            modelAndView.addObject(out);
        }
        //在这里创建的mail工具类没有使用静态方法，
//        MailUtil mailUtil = new MailUtil();
        /**
         * 发送普通文本邮件
         * @param to 收件人
         * @param subject 主题
         * @param content 内容
         */
        String to  = email;
        String subject = "发送普通文本邮件";
        String content = "测试---发送普通文本邮件";
        try {
            mailService.sendSimpleMail(to,subject,content);
//            mailService.sendSimpleMail("13461604051@163.com", "发送邮件测试", "大家好，这是我用springboot进行发送邮件测试")
//            modelAndView.setViewName("/index");
            //           下面代码是为了方便开发
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('发送普通文本邮件[成功]！')</script>");
            modelAndView.addObject(out);
            return modelAndView;
        } catch (Exception o){
//            modelAndView.setViewName("/index");
            //           下面代码是为了方便开发
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('发送普通文本邮件[异常]！')</script>");
            modelAndView.addObject(out);
            return modelAndView;
        }

    }


    /**
     * 发送HTML邮件
     * @return
     */
    @RequestMapping(value = "/sendHtmlMail", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView sendHtmlMail(@RequestParam("email") String email,HttpServletResponse response) throws IOException {
        ModelAndView modelAndView= new ModelAndView();
        String content = "<html><body><h3><font color=\"red\">" + "大家好，这是springboot发送的HTML邮件" + "</font></h3></body></html>";
        mailService.sendHtmlMail(email, "发送邮件测试", content);

        modelAndView.setViewName("/index");
        //           下面代码是为了方便开发
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.print("<script language=\"javascript\">alert('发送HTML邮件[成功]！')</script>");
        modelAndView.addObject(out);
        return modelAndView;
    }


    /**
     * 发送带附件的邮件
     */
    @RequestMapping(value = "/sendAttachmentMail", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView sendAttachmentMail(@RequestParam("email") String email,HttpServletResponse response) throws IOException {
        ModelAndView modelAndView= new ModelAndView();
        String content = "<html><body><h3><font color=\"red\">" + "大家好，这是springboot发送的HTML邮件，有附件哦" + "</font></h3></body></html>";
        String filePath = "C:\\Users\\Z-C-Y\\Pictures\\P021.jpg";
        mailService.sendAttachmentMail(email, "发送带附件的邮件测试", content, filePath);

        modelAndView.setViewName("/index");
        //           下面代码是为了方便开发
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.print("<script language=\"javascript\">alert('发送带附件的邮件[成功]！')</script>");
        modelAndView.addObject(out);
        return modelAndView;
    }

    /**
     * 发送带图片的邮件
     */
    @RequestMapping(value = "/sendInlineResourceMail", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView sendInlineResourceMail(@RequestParam("email") String email,HttpServletResponse response) throws IOException {
        ModelAndView modelAndView= new ModelAndView();
        String rscPath = "C:\\Users\\Z-C-Y\\Pictures\\P021.jpg";
        String rscId = "P021";
        String content = "<html><body><h3><font color=\"red\">" + "大家好，这是springboot发送的HTML邮件，有图片哦" + "</font></h3>"
                + "<img src=\'cid:" + rscId + "\'></body></html>";
        mailService.sendInlineResourceMail(email, "发送带图片的邮件", content, rscPath, rscId);
        modelAndView.setViewName("/index");
        //           下面代码是为了方便开发
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.print("<script language=\"javascript\">alert('发送带图片的邮件[成功]！')</script>");
        modelAndView.addObject(out);
        return modelAndView;
    }




}
